from docx2py import readDocx
from py2docx import writeDocx
import docx
from docx.enum.style import WD_STYLE_TYPE
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.shared import Inches, Pt
from docx.oxml.ns import qn
import json
readStyle = None
readDocxFile = None
fontSizeDict = {'key':[],'value':[]}
for key,v in json.load(open('convert/fontSize.json',encoding='utf-8')).items():
	fontSizeDict['key'].append(key)
	fontSizeDict['value'].append(v)

def findFontSize(by,target,value):
	if value in fontSizeDict[by]:
		return fontSizeDict[target][fontSizeDict[by].index(value)]
	else: return None

def importDocx(docxname):
	global readStyle
	global readDocxFile
	readDocxFile = docx.Document(docxname)
	readStyle,pickStyles = readDocx(readDocxFile)
	config = {'styles': [],'allSet':[]}
	for each in pickStyles:
		print('pick styles',each)
		style = each['style']
		style = transStyleName(style)
		if config['styles'].count(style) == 0:
			config['styles'].append(style)
			out = {'style': style}
			if each.get('font') is not None:
				out['Font'] = each['font']['eastAsia']
			if each.get('fontSize') is not None:
				out['FontSize'] = findFontSize('value','key',each['fontSize'])
			if each.get('bold') is not None:
				out['bold'] = each['bold']
			if each.get('alignment') is not None:
				out['Alignment'] = each['alignment']
			config['allSet'].append(out)
	for each in readStyle:
		print('read styles',each)
		style = each['paragraph']['style']
		style = transStyleName(style)
		if config['styles'].count(style) == 0:
			config['styles'].append(style)
	return config


def transStyleName(name):
	if name == 'Normal':
		return '正文'
	else:
		return name


def exportDocx(docxname,styles):
	targetStyle = styles
	print('从docx中获取的style')
	for each in readStyle:
		print(each)
	print('从用户选择中获取的style',targetStyle)
	doc = docx.Document()
	for ets in targetStyle:
		createStyle(doc,ets)
	for eOriginStyle in readDocxFile.styles:
		if eOriginStyle.name not in doc.styles:
			if isinstance(eOriginStyle,docx.styles.style._ParagraphStyle):
				style = doc.styles.add_style(eOriginStyle.name, WD_STYLE_TYPE.PARAGRAPH)
				style = eOriginStyle
	writeDocx(doc,readStyle)
	doc.save(docxname)

def createStyle(document,targetStyle):
	style = document.styles.add_style(targetStyle['style'], WD_STYLE_TYPE.PARAGRAPH)
	font = style.font
	font.name = targetStyle.get('Font')
	font.size = findFontSize('key','value',targetStyle.get('FontSize'))
	font.bold = targetStyle.get('bold')
	font.underline = targetStyle.get('underline')
	font.italic = targetStyle.get('italic')
	paragraph_format = style.paragraph_format
	alignment = targetStyle.get('Alignment')
	if alignment == '靠左对齐': 
		paragraph_format.alignment = WD_ALIGN_PARAGRAPH.LEFT
	elif alignment == '靠右对齐':
		paragraph_format.alignment = WD_ALIGN_PARAGRAPH.RIGHT
	elif alignment == '居中对齐':
		paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
	elif alignment == '两端对齐':
		paragraph_format.alignment = WD_ALIGN_PARAGRAPH.DISTRIBUTE
	# paragraph_format.left_indent = Inches(0.25)
	# paragraph_format.first_line_indent = Inches(-0.25)
	# paragraph_format.space_before = Pt(12)